package com.kyo.codec;

public class HexUtil {
	// 空间换时间
	private static final char[] hexChars = new char[] { '0', '1', '2', '3',
			'4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

	public static String toHexString(byte[] bytes) {
		if (bytes == null || bytes.length == 0) {
			return null;
		}
		StringBuilder builder = new StringBuilder(bytes.length * 2);
		int i = 0;
		for (byte b : bytes) {
			int low = b & 0x0F;
			int high = (b >> 4) & 0X0F;
			builder.append(hexChars[high]).append(hexChars[low]).append(' ');
			i++;
			if (i % 30 == 0) {
				builder.append('\n');
			}
		}
		return builder.toString();
	}
}
